import java.util.PriorityQueue;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Hua YY
 * Date: 2025-01-23
 * Time: 13:43
 */
class KthLargest2 {
    //创建全局成员变量
    private PriorityQueue<Integer> heap;//默认为小根堆
    private int _k;//堆的大小

    public KthLargest2(int k, int[] nums) {
        heap = new PriorityQueue<>((a,b) -> a-b);
        _k = k;
        for(int i = 0 ; i < nums.length ; i++){
            heap.offer(nums[i]);
            if(heap.size() > _k){
                heap.poll();
            }
        }
    }

    public int add(int val) {
        heap.offer(val);
        if(heap.size() > _k){
            heap.poll();
        }
        return heap.peek();
    }
}

/**
 * Your KthLargest object will be instantiated and called as such:
 * KthLargest obj = new KthLargest(k, nums);
 * int param_1 = obj.add(val);
 */
public class Test2 {
}
